package com.codeplatform;

import com.codeplatform.util.ColorLogger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 多语言在线编译平台主应用程序
 * 
 * @author CodePlatform Team
 * @version 2.0.0
 * @since 2024-01-01
 */
@SpringBootApplication
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
public class CodePlatformApplication {

    @Resource
    private Environment environment;

    /**
     * 应用程序主入口
     * 
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        // 启动前日志
        ColorLogger.separator("多语言在线编译平台启动中");
        ColorLogger.startup("Spring Boot应用", "正在启动...");
        
        SpringApplication.run(CodePlatformApplication.class, args);
    }

    /**
     * 应用启动完成事件监听
     */
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        try {
            String serverPort = environment.getProperty("server.port", "8080");
            String contextPath = environment.getProperty("server.servlet.context-path", "");
            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            
            ColorLogger.separator("应用启动成功");
            ColorLogger.startup("服务器", "已启动");
            ColorLogger.info("本地访问地址: %s", ColorLogger.highlightSuccess("http://localhost:" + serverPort + contextPath));
            ColorLogger.info("网络访问地址: %s", ColorLogger.highlightSuccess("http://" + hostAddress + ":" + serverPort + contextPath));
            ColorLogger.info("API文档地址: %s", ColorLogger.highlight("http://localhost:" + serverPort + contextPath + "/doc.html"));
            ColorLogger.info("健康检查地址: %s", ColorLogger.highlight("http://localhost:" + serverPort + contextPath + "/actuator/health"));
            
            // 显示活跃的配置文件
            String[] activeProfiles = environment.getActiveProfiles();
            if (activeProfiles.length > 0) {
                ColorLogger.info("活跃配置文件: %s", ColorLogger.highlight(String.join(", ", activeProfiles)));
            } else {
                ColorLogger.info("活跃配置文件: %s", ColorLogger.highlight("default"));
            }
            
            ColorLogger.separator("");
            ColorLogger.success("🎉 多语言在线编译平台启动完成！");
            
        } catch (UnknownHostException e) {
            ColorLogger.error("获取主机地址失败", e);
        }
    }

    /**
     * 应用关闭事件监听
     */
    @EventListener(ContextClosedEvent.class)
    public void onApplicationShutdown() {
        ColorLogger.separator("应用正在关闭");
        ColorLogger.shutdown("Spring Boot应用", "正常关闭");
        ColorLogger.info("感谢使用多语言在线编译平台！");
        ColorLogger.separator("");
    }
}